package com.sunspring.jbug.controller;

import com.sunspring.jbug.bean.OperationLog;
import com.sunspring.jbug.bean.UserInfo;
import com.sunspring.jbug.bean.VersionControl;
import com.sunspring.jbug.common.LogEnum;
import com.sunspring.jbug.common.SimpleResponse;
import com.sunspring.jbug.service.OperationLogService;
import com.sunspring.jbug.service.VersionService;
import com.sunspring.jbug.support.ContextHolder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @author wulei
 * @date 2019-03-06
 **/
@RestController
@RequestMapping("/version")
public class VersionController {


    @Autowired
    private VersionService versionService;

    @Autowired
    private OperationLogService ols;

    /**
     * 创建新版本
     */
    @PostMapping("/create")
    public SimpleResponse create(VersionControl versionControl) {
        versionService.create(versionControl);
        UserInfo currentUser = ContextHolder.get();
        OperationLog operationLog = OperationLog.builder()
                .type(LogEnum.PROJECT.getCode())
                .aboutId(versionControl.getProjectId())
                .message(currentUser.getName() + "创建了新版本：" + versionControl.getVersionNumber())
                .build();
        ols.addLog(operationLog);
        return SimpleResponse.success();
    }

    /**
     * 查看项目所有版本
     * @param projectId 项目Id
     * @return VersionControl list
     */
    @GetMapping("/byProject")
    public SimpleResponse byProject(Integer projectId) {
        return SimpleResponse.success(versionService.getVersion(projectId));
    }

    /**
     * 删除版本号
     * @param versionId 版本号
     */
    @DeleteMapping("/delete")
    public SimpleResponse delete(Integer versionId) {
        versionService.deleteVersion(versionId);
        return SimpleResponse.success();
    }
}
